package com.raptor.logs;


import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.spi.AppenderAttachable;

import java.util.HashMap;
import java.util.Map;

public abstract class KafkaAppenderConfig<E> extends UnsynchronizedAppenderBase<E> implements AppenderAttachable<E> {

//    protected String topic = null;

    protected Encoder<E> encoder = null;
    protected String logType = "Other";
//    protected KeyingStrategy<? super E> keyingStrategy = null;
//    protected DeliveryStrategy deliveryStrategy;

//    protected Integer partition = null;

    //    protected boolean appendTimestamp = true;
    protected Map<String, Object> producerConfig = new HashMap<String, Object>();

    public String getLogType() {
        return logType;
    }

    public void setLogType(String logTye) {
        this.logType = logTye;
    }


    protected boolean checkPrerequisites() {
        boolean errorFree = true;

        if (producerConfig.get("bootstrap.servers") == null) {
            addError("No \"" + "BOOTSTRAP_SERVERS_CONFIG " + "\" set for the appender named [\""
                    + name + "\"].");
            errorFree = false;
        }

//        if (topic == null) {
//            addError("No topic set for the appender named [\"" + name + "\"].");
//            errorFree = false;
//        }

        if (logType == null || logType == "") {
            addError("No \"" + "LogType " + "\" set for the appender named [\""
                    + name + "\"].");
            errorFree = false;
        }

        if (encoder == null) {
            addError("No encoder set for the appender named [\"" + name + "\"].");
            errorFree = false;
        }

//        if (keyingStrategy == null) {
//            addInfo("No explicit keyingStrategy set for the appender named [\"" + name + "\"]. Using default NoKeyKeyingStrategy.");
//            keyingStrategy = new NoKeyKeyingStrategy();
//        }
//
//        if (deliveryStrategy == null) {
//            addInfo("No explicit deliveryStrategy set for the appender named [\""+name+"\"]. Using default asynchronous strategy.");
//            deliveryStrategy = new AsynchronousDeliveryStrategy();
//        }

        return errorFree;
    }

    public void setEncoder(Encoder<E> encoder) {
        this.encoder = encoder;
    }

//    public void setKeyingStrategy(KeyingStrategy<? super E> keyingStrategy) {
//        this.keyingStrategy = keyingStrategy;
//    }

    public void addProducerConfig(String keyValue) {
        String[] split = keyValue.split("=", 2);
        if (split.length == 2)
            addProducerConfigValue(split[0], split[1]);
    }

    public void addProducerConfigValue(String key, Object value) {
        this.producerConfig.put(key, value);
    }

    public Map<String, Object> getProducerConfig() {
        return producerConfig;
    }
}
